
package picturemouse;

import java.io.Serializable;
import java.util.Arrays;
import java.util.ArrayList;

/**
*
* @author Group D
*/
public class Screening implements Serializable {
    
    private final int screeningID;
    private final String filmName;
    private final String time;
    private final int screenNum;
    private final ArrayList<Integer> bookedSeats;
    
    public Screening(int screeningID, String filmName, String time, int screenNum) {
        this.screeningID = screeningID;
        this.filmName = filmName;
        this.time = time;
        this.screenNum = screenNum;
        bookedSeats = new ArrayList<>();
    }
    
    /**
    * Method to return an array of seat IDs for the avaliable seats for the screening
     * @return Array of Integers representing seat IDs
    */
    public Integer[] listAvaliableSeats(PictureMouse pictureMouse) {
        ArrayList<Integer> avaliableSeats = new ArrayList<>();
        //Get the list of seat IDs for the screen
        Integer[] screenSeats = pictureMouse.getSeatsByScreenNum(screenNum);
        //Loop through each seatID
        for (Integer seatID : screenSeats) {
            boolean seatBooked = false;
            //Loop through the list of booked seats, if the seat doesn't exists in the list, add it to the list
            for (Integer bookedSeatID : bookedSeats) {
                if (bookedSeatID==seatID) seatBooked = true;
            }
            if (!seatBooked) avaliableSeats.add(seatID);
        }
        //Return the list in array form
        return avaliableSeats.toArray(new Integer[0]);
    }
    
    /**
     * Method to book a seat for this screening
     * @param seatID
     * @throws SeatNotAvaliableException
     */
    public void bookSeat(Integer seatID, PictureMouse pictureMouse) throws SeatNotAvaliableException {
        boolean seatAvaliable = false;
        //Get the list of seat id's for the screen
        Integer[] avaliableSeats = this.listAvaliableSeats(pictureMouse);
        for (Integer avaliableSeatID : avaliableSeats) {
            //For each avaliable seat, check if it matches the supplied seatID
            if (avaliableSeatID==seatID) seatAvaliable = true;
        }
        //If the supplied seatID wasn't found in the screen seat list, throw an exception
        if (!seatAvaliable) throw new SeatNotAvaliableException();
        //Add the seat to the list of booked seats for this screening
        bookedSeats.add(seatID);
    }
    
    public boolean seatAvaliable(int seatID, PictureMouse server) {
        boolean seatAvaliable = false;
        //Get the list of seat id's for the screen
        Integer[] avaliableSeats = this.listAvaliableSeats(server);
        for (Integer avaliableSeatID : avaliableSeats) {
            //For each avaliable seat, check if it matches the supplied seatID
            if (avaliableSeatID==seatID) seatAvaliable = true;
        }
        //If the supplied seatID wasn't found in the screen seat list, throw an exception
        return seatAvaliable;
    }
    
    /**
    * Method to return the ID of the screening
     * @return ID of the seat associated with screening
    */
    public int getID() {
        return screeningID;
    }
    
    /**
    * Method to return the film name of the screening
     * @return Name of film associated with this screening
    */
    public String getFilmName() {
        return filmName;
    }
    
    /**
    * Method to return the Time of the screening
     * @return Time and Date associated with this screening
    */
    public String getTime() {
        return time;
    }
    
    /**
    * Method to return the screen number of the screening
     * @return Screen number associated with this screening
    */
    public int getScreenNum() {
        return screenNum;
    }
    
}
